package com.example.game;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class GameActivity extends Activity {
    Button[] bt = new Button[9];
    int hide = 8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        setTitle("��ʼ��Ϸ");
        bt[0] = (Button) findViewById(R.id.button1);
        bt[1] = (Button) findViewById(R.id.button2);
        bt[2] = (Button) findViewById(R.id.button3);
        bt[3] = (Button) findViewById(R.id.button4);
        bt[4] = (Button) findViewById(R.id.button5);
        bt[5] = (Button) findViewById(R.id.button6);
        bt[6] = (Button) findViewById(R.id.button7);
        bt[7] = (Button) findViewById(R.id.button8);
        bt[8] = (Button) findViewById(R.id.button9);
        for (int i = 0; i < bt.length; i++) {
            bt[i].setText(String.valueOf(i));
            bt[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    // ((Button)arg0).setVisibility(View.INVISIBLE);
                    String index = String.valueOf(((Button) arg0).getText());
                    // int num = Integer.valueOf(index);

                    int click;
                    switch (arg0.getId()) {
                        case R.id.button1:
                            click = 0;
                            break;
                        case R.id.button2:
                            click = 1;
                            break;
                        case R.id.button3:
                            click = 2;
                            break;
                        case R.id.button4:
                            click = 3;
                            break;
                        case R.id.button5:
                            click = 4;
                            break;
                        case R.id.button6:
                            click = 5;
                            break;
                        case R.id.button7:
                            click = 6;
                            break;
                        case R.id.button8:
                            click = 7;
                            break;
                        case R.id.button9:
                            click = 8;
                            break;
                        default:
                            click = -1;
                            break;
                    }
                    // setTitle(String.valueOf(click)+","+String.valueOf(hide));
                    if (isNeighbor(hide, click)) {
                        String temp;
                        temp = (String) bt[hide].getText();
                        bt[hide].setVisibility(View.VISIBLE);
                        bt[hide].setText(index);
                        bt[click].setVisibility(View.INVISIBLE);
                        bt[click].setText(temp);
                        hide = click;
                    }
                    int j;
                    for (j = 0; j < bt.length; j++) {
                        if (j != Integer.valueOf((String) bt[j].getText()))
                            break;
                    }
                    if (j == 9) {
                        // setTitle("Cons!��Ӯ��!");
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(
                                GameActivity.this);
                        builder1.setTitle("��ϲ");
                        builder1.setMessage("����һ�֣�");
                        builder1.setPositiveButton("��",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface arg0,
                                                        int arg1) {
                                        // TODO Auto-generated method stub
                                        setTitle("ȷ��");
                                    }
                                });
                        builder1.setNegativeButton("������",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface arg0,
                                                        int arg1) {
                                        // TODO Auto-generated method stub
                                        setTitle("������");
                                        finish();
                                    }
                                });
                        builder1.show();
                    } else {
                        setTitle("Come on!����!");
                    }
                }
            });
        }
        bt[8].setVisibility(View.INVISIBLE);

    }

    public boolean isNeighbor(int hide, int click) {
        boolean result = false;
        int hide_c, hide_r, click_c, click_r;
        hide_c = hide % 3;
        hide_r = hide / 3;
        click_c = click % 3;
        click_r = click / 3;
        if ((hide_r == click_r && Math.abs(hide_c - click_c) == 1)
                ^ (hide_c == click_c && Math.abs(hide_r - click_r) == 1))
            result = true;
        return result;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_quit) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
